package apresentacao;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Color;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import persistencia.Conexao;
import persistencia.DaoMidia;
import dominio.Midia;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;
import javax.swing.JScrollPane;

public class TelaCadastroMidia extends JDialog {
	private JTextField textField_Nome;
	private JTextField textField_Setor;
	private JTextField textField_PrecoCompra;
	private JTextField textField_Interprete;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			TelaCadastroMidia dialog = new TelaCadastroMidia();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public TelaCadastroMidia() {
		getContentPane().setBackground(new Color(240, 230, 140));
	setBounds(100, 100, 641, 458);
	getContentPane().setLayout(null);
	
	JLabel lblNewLabel = new JLabel("Cadastro - M\u00EDdia");
	lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
	lblNewLabel.setFont(new Font("Arial", Font.BOLD, 20));
	lblNewLabel.setBounds(10, 11, 605, 34);
	getContentPane().add(lblNewLabel);
	
	JPanel panel = new JPanel();
	panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
	panel.setBackground(new Color(238, 232, 170));
	panel.setBounds(10, 71, 297, 337);
	getContentPane().add(panel);
	panel.setLayout(null);
	
	JLabel lbl_nome = new JLabel("Nome :");
	lbl_nome.setFont(new Font("Arial", Font.BOLD, 13));
	lbl_nome.setBounds(23, 11, 54, 21);
	panel.add(lbl_nome);
	
	textField_Nome = new JTextField();
	textField_Nome.setFont(new Font("Arial", Font.PLAIN, 12));
	textField_Nome.setColumns(10);
	textField_Nome.setBounds(23, 31, 248, 27);
	panel.add(textField_Nome);
	
	JLabel lbl_descricao = new JLabel("Descri\u00E7\u00E3o :");
	lbl_descricao.setFont(new Font("Arial", Font.BOLD, 13));
	lbl_descricao.setBounds(23, 74, 70, 21);
	panel.add(lbl_descricao);
	
	JScrollPane scrollPane = new JScrollPane();
	scrollPane.setBounds(25, 95, 246, 96);
	panel.add(scrollPane);
	
	final JTextArea textArea_Descricao = new JTextArea();
	scrollPane.setViewportView(textArea_Descricao);
	textArea_Descricao.setWrapStyleWord(true);
	textArea_Descricao.setLineWrap(true);
	textArea_Descricao.setFont(new Font("Arial", Font.PLAIN, 12));
	
	JLabel lbl_corredor = new JLabel("Corredor :");
	lbl_corredor.setFont(new Font("Arial", Font.BOLD, 13));
	lbl_corredor.setBounds(23, 207, 70, 21);
	panel.add(lbl_corredor);
	
	JLabel lbl_setor = new JLabel("Setor :");
	lbl_setor.setFont(new Font("Arial", Font.BOLD, 13));
	lbl_setor.setBounds(100, 207, 46, 21);
	panel.add(lbl_setor);
	
	final JComboBox comboBox_Corredor = new JComboBox();
	comboBox_Corredor.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6"}));
	comboBox_Corredor.setFont(new Font("Arial", Font.PLAIN, 12));
	comboBox_Corredor.setBounds(23, 228, 70, 27);
	panel.add(comboBox_Corredor);
	
	textField_Setor = new JTextField();
	textField_Setor.setFont(new Font("Arial", Font.PLAIN, 12));
	textField_Setor.setColumns(10);
	textField_Setor.setBounds(100, 229, 171, 27);
	panel.add(textField_Setor);
	
	JLabel lbl_precoCompra = new JLabel("Pre\u00E7o de Compra :");
	lbl_precoCompra.setFont(new Font("Arial", Font.BOLD, 13));
	lbl_precoCompra.setBounds(23, 267, 123, 25);
	panel.add(lbl_precoCompra);
	
	textField_PrecoCompra = new JTextField();
	textField_PrecoCompra.setColumns(10);
	textField_PrecoCompra.setBounds(46, 290, 91, 27);
	panel.add(textField_PrecoCompra);
	
	JLabel lbl_RS = new JLabel("R$");
	lbl_RS.setFont(new Font("Tahoma", Font.PLAIN, 12));
	lbl_RS.setBounds(23, 295, 27, 14);
	panel.add(lbl_RS);
	
	JPanel panel_1 = new JPanel();
	panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
	panel_1.setBackground(new Color(238, 232, 170));
	panel_1.setBounds(341, 71, 274, 274);
	getContentPane().add(panel_1);
	panel_1.setLayout(null);
	
	JLabel lbl_interprete = new JLabel("Int\u00E9rprete :");
	lbl_interprete.setFont(new Font("Arial", Font.BOLD, 13));
	lbl_interprete.setBounds(31, 11, 80, 22);
	panel_1.add(lbl_interprete);
	
	textField_Interprete = new JTextField();
	textField_Interprete.setBounds(31, 32, 209, 27);
	panel_1.add(textField_Interprete);
	textField_Interprete.setColumns(10);
	
	JLabel lbl_anoProducao = new JLabel("Ano de Produ\u00E7\u00E3o :");
	lbl_anoProducao.setFont(new Font("Arial", Font.BOLD, 13));
	lbl_anoProducao.setBounds(31, 73, 130, 22);
	panel_1.add(lbl_anoProducao);
	
	final JComboBox comboBox_AnoProducao = new JComboBox();
	comboBox_AnoProducao.setFont(new Font("Arial", Font.PLAIN, 12));
	comboBox_AnoProducao.setModel(new DefaultComboBoxModel(new String[] {"1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004"}));
	comboBox_AnoProducao.setBounds(31, 96, 64, 27);
	panel_1.add(comboBox_AnoProducao);
	
	JLabel lbl_tipo = new JLabel("Tipo :");
	lbl_tipo.setFont(new Font("Arial", Font.BOLD, 13));
	lbl_tipo.setBounds(31, 135, 46, 22);
	panel_1.add(lbl_tipo);
	
	final JComboBox comboBox_Tipo = new JComboBox();
	comboBox_Tipo.setModel(new DefaultComboBoxModel(new String[] {"CD", "DVD"}));
	comboBox_Tipo.setBounds(31, 161, 105, 27);
	panel_1.add(comboBox_Tipo);
	
	JLabel lbl_genero = new JLabel("G\u00EAnero :\r\n");
	lbl_genero.setFont(new Font("Arial", Font.BOLD, 13));
	lbl_genero.setBounds(31, 199, 64, 21);
	panel_1.add(lbl_genero);
	
	textField = new JTextField();
	textField.setBounds(31, 221, 130, 27);
	panel_1.add(textField);
	textField.setColumns(10);
	
	JButton btn_Salvar = new JButton("Salvar");
	btn_Salvar.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			Midia midia = new Midia();
			Connection conn = null;
			try
			{
				Conexao bd = new Conexao();
				DaoMidia daoMidia = new DaoMidia();
				conn = bd.obtemConexao();
				
				conn.setAutoCommit(false);
				
				midia.setIdtipoproduto(1);
				midia.setNome(textField_Nome.getText());
				midia.setDescricao(textArea_Descricao.getText());
				midia.setCorredor((String)comboBox_Corredor.getSelectedItem().toString());
				midia.setSetor(textField_Setor.getText());
				midia.setPrecocompra(Float.parseFloat(textField_PrecoCompra.getText()));
				midia.setInterprete(textField_Interprete.getText());
				midia.setAnoproducao((String)comboBox_AnoProducao.getSelectedItem().toString());
				midia.setTipo((String)comboBox_Tipo.getSelectedItem().toString());
				midia.setGenero(textField.getText());
		
				daoMidia.incluir(conn, midia);
				conn.commit();
		        JOptionPane.showMessageDialog(rootPane, "Item cadastrado com sucesso!");
		        
		        textField_Nome.setText("");
		        textArea_Descricao.setText("");
		        comboBox_Corredor.setSelectedItem("1");
		        textField_Setor.setText("");
		        textField_PrecoCompra.setText("");
		        textField_Interprete.setText("");
		        comboBox_AnoProducao.setSelectedItem("1995");
		        comboBox_Tipo.setSelectedItem("CD");
		        textField.setText("");
	        
			}
			catch (Exception ex) {
	            ex.printStackTrace();
	            if (conn != null) {
	               try {
	                  conn.rollback();
	               }
	                   catch (SQLException e1) {
	                     System.out.print(e1.getStackTrace());
	                  }
	            }
	        }
			finally {
	         if (conn != null) {
	            try {
	               conn.close();
	            }
	                catch (SQLException e1) {
	                  System.out.print(e1.getStackTrace());
	               }
	         }
			}
		
		}
		
	});
	
	btn_Salvar.setFont(new Font("Arial", Font.BOLD, 14));
	btn_Salvar.setBounds(457, 375, 84, 33);
	getContentPane().add(btn_Salvar);
	
	JButton btnNewButton = new JButton("Voltar");
	btnNewButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	});
	btnNewButton.setFont(new Font("Arial", Font.PLAIN, 12));
	btnNewButton.setBounds(545, 375, 70, 33);
	getContentPane().add(btnNewButton);
	}
}
